<?php

App::uses('AppController', 'Controller');

class RepertoireController extends AppController {
    
    public $uses = array('Repertoire');
    
    public $paginate = array(
        'limit' => ITEMS_PER_PAGE
    );
    
    public function index() {
        $repertoire = $this->paginate('Repertoire');
        
        $this->set(compact('repertoire'));
    }
    
    public function create() {
        $this->set('data', $this->Repertoire->create());
        
        $this->render('form');
    }
    
    public function edit($id) {
        $this->set('data', $this->Repertoire->findById($id));
        
        $this->render('form');
    }
    
    public function save() {
        $data = $this->data;
        if (! empty($data)) {
            $this->Repertoire->create($data);
            
            if (! $this->Repertoire->validates()) {
                $this->set('data', $data);
                $this->setFlash(strtotitle(__('save failed')) . ". " . strtotitle(__('invalid data')) . ".");
                return $this->render('form');
            }
            
            if (! $this->Repertoire->save($data)) {
                $this->set('data', $data);
                $this->setFlash(strtotitle(__('save failed')));
                return $this->render('form');
            }
            
            $this->setFlash(__('saved'), 'ok');
            $this->redirect('edit/'.$this->Repertoire->id);
        }
        
        $this->redirect('index');
    }
    
    public function delete($id) {
        $this->Repertoire->delete($id);
        $this->redirect('index');
    }
    
}
